package com.army.study.database;

import com.army.study.bean.ChartPoint;
import com.army.study.utils.ChartDataUtils;
import com.army.study.utils.DateUtils;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡片数据库操作
 */
public class DatabaseUtils {
    private static final int SHOW_DAYS = 3;
    private static final String DATA_FILED_DATE = "date";

    public DatabaseUtils() {
    }

    /**
     * 新增卡片信息
     * @param form 卡片对象
     * @param connect 数据连接
     */
    public static void insertForm(Form form, OrmContext connect) {
        connect.insert(form);
        connect.flush();
    }

    /**
     * 插入步数到数据库
     * @param value
     * @param connect
     */
    public static void insertValues(float value, OrmContext connect) {
        String now = DateUtils.getDate(0);
        Sensor sensorData = new Sensor(now, (int)value);
        // 查询今天是否有数据
        Sensor todayData = getSensorData(connect, now);
        boolean isInsert = false;
        if (null == todayData) {
            isInsert = connect.insert(sensorData);
        }else {
            todayData.setStepsValue(sensorData.getStepsValue());
            isInsert = connect.update(todayData);
        }
        if (isInsert) {
            connect.flush();
        }

    }

    /**
     * 获取真实的当天步数
     * @param connect
     * @param date
     * @return
     */
    public static Sensor getSensorData(OrmContext connect, String date) {
        OrmPredicates ormPredicates = new OrmPredicates(Sensor.class);
        ormPredicates.equalTo(DATA_FILED_DATE, date); // 条件为date字段相等
        List<Sensor> datas = connect.query(ormPredicates);
        Sensor sensorData = null;
        if (datas.size() > 0) {
            sensorData = datas.get(0);
        }
        return sensorData;
    }

    /**
     * 删除数据
     * @param formId 卡片ID
     * @param connect 数据连接
     */
    public static void deleteFormData(long formId, OrmContext connect) {
        OrmPredicates where = connect.where(Form.class);
        where.equalTo("formId", formId);
        List<Form> forms = connect.query(where);
        if (!forms.isEmpty() && forms.size() > 0) {
            connect.delete(forms.get(0));
            connect.flush();
        }
    }

    /**
     * 获取真实的当天步数
     * @param value 步数
     * @param connect 数据库连接
     * @param now 今天日期
     * @param yesterday 昨天日期
     * @return 返回真实步数
     */
    public static Sensor getRealSensorData(float value, OrmContext connect, String now, String yesterday) {
        OrmPredicates ormPredicates = new OrmPredicates(Sensor.class);
        ormPredicates.equalTo(DATA_FILED_DATE, yesterday);
        List<Sensor> datas = connect.query(ormPredicates);
        Sensor sensorData = null;
        if (datas.size() > 0 ) {
            sensorData = new Sensor(now, (int)(value - datas.get(0).getStepsValue()));
        }else{
            sensorData = new Sensor(now, (int)value);
        }
        return sensorData;
    }

    /**
     * 获取前三天的数据
     * @param connect 数据连接
     * @return 数据
     */
    public static List<ChartPoint> getLastFourDaysValue(OrmContext connect) {
        List<ChartPoint> resuts = new ArrayList<>(SHOW_DAYS);
        OrmPredicates ormPredicates = new OrmPredicates(Sensor.class);
        for (int i = SHOW_DAYS; i > 0; i--) {
            ormPredicates.equalTo(DATA_FILED_DATE, DateUtils.getDate(i));
            List<Sensor> datas = connect.query(ormPredicates);
            if (datas.size() == 0) {
                resuts.add(ChartDataUtils.getChartPoint(0));
            }else {
                resuts.add(ChartDataUtils.getChartPoint(datas.get(0).getStepsValue()));
            }
        }

        return resuts;
    }
}
